﻿#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void Isit_Board(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void Display_Board(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		printf("---|---|---\n");
	}
	printf("\n");
}

void Player(char board[ROW][COL], int row, int col)
{
	while (1)
	{
		printf("玩家轮次->");
		int x = 0;
		int y = 0;
		scanf("%d%d", &x, &y);
		if (x > 0 && x <= col && y > 0 && y <= row)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else 
			{
				printf("该坐标被占用！\n");
			}
		}
		else
		{
			printf("输入错误\n");
		}
	}
}

void ComputerPlayer(char board[ROW][COL], int row, int col)
{
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (x > 0 && x <= col && y > 0 && y <= row)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '#';
				break;
			}
		/*	else
			{
				printf("该坐标被占用！\n");
			}*/
		}
		//else
		//{
		//	printf("输入错误\n");
		//}
	}
}

int IsFull(char board[ROW][COL], int row, int col)
{
	int x, y = 0;
	for (x = 0; x < row; x++)
	{
		for (y = 0; y < col; y++)
		{
			if (board[x][y] == ' ')
				return 0;
		}
	}
	return 1;
}

char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//X
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
			return board[i][1];
	}
	//Y
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
			return board[1][i];
	}
	//x
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	if (board[1][3] == board[1][1] && board[1][1] == board[3][1] && board[1][1] != ' ')
		return board[1][1];
	if (1 == IsFull(board, ROW, COL))
	{
		return 'q';
	}
	else
		return 'c';
}

void game()
{
	char board[ROW][COL] = { 0 };
	//初始化
	Isit_Board(board, ROW, COL);
	//显示棋盘
	Display_Board(board, ROW, COL);

	char ch = '0';
	while (1)
	{
		//玩家先走
		Player(board, ROW, COL);
		Display_Board(board, ROW, COL);
		ch = IsWin(board, ROW, COL);
		if (ch != 'c')
		{
			break;
		}
		ComputerPlayer(board, ROW, COL);
		Display_Board(board, ROW, COL);
		IsWin(board, ROW, COL);
		if (ch != 'c')
		{
			break;
		}
	}
	if (ch == '*')
	{
		printf("玩家赢！\n");
	}
	if (ch == '#')
	{
		printf("人机赢！\n");
	}
	//继续
	if (ch == 'q')
	{
		printf("平局\n");
	}
}

void menu()
{
	printf("********************************\n");
	printf("********* 1. 三子棋  ***********\n");
	printf("********* 2. 退  出  ***********\n");
	printf("********************************\n");
	
}
void play()
{
	while (1)
	{
		menu();
		int a = 0;
		scanf("%d", &a);
		switch (a)
		{
		case 1:
			game();
			break;
		case 2:
			printf("已退出游戏\n");
			break;
		default:
			printf("输入错误\n");
			break;
		}
		if (a == 2)
			break;
	}
}

int main()
{
	srand((unsigned)time(NULL));
	
	play();
	return 0;
}